package com.example.asproject1801_1.requestpermisson;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import static android.os.Build.VERSION_CODES.M;

import com.example.asproject1801_1.R;

/*申请动态权限的步骤：
 *  1.在清单文件当中，声明权限的申请
 *
 * */
public class CallActivity extends AppCompatActivity {
    int rCode = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call);
    }

    public void onClick(View view) {
//        callPhone("10086");
//        检查权限是否被申请
        checkPermission();
    }

    /* 核对项目当中，用户是否给与了打电话的权限*/
    private void checkPermission() {
//        判断当前手机的安卓版本号，如果大于等于6.0，则需要申请权限，否则不用申请权限
        if (Build.VERSION.SDK_INT >= M) {
//            sdk的版本大于6.0，要检查是否有打电话的权限
            int checkSelfPermission = ContextCompat.checkSelfPermission(this,
                    Manifest.permission.CALL_PHONE);
            if (checkSelfPermission == PackageManager.PERMISSION_GRANTED) {
//                判断用户是否授予这个权限，此时说明已经获得了权限
                callPhone("10086");
            } else {
//                说明此时没有获取到这个权限，申请权限
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.CALL_PHONE}, rCode);
            }
        } else {
            callPhone("10086");
        }
    }

    /*
     * 此函数是申请权限的回调方法，无论成功还是失败，都会调用这个函数
     * requestCode： 上文提到的请求码
     * permissions ：申请权限的数组
     * grantResults  ：权限申请对应的结果
     * */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == rCode) {
            if (grantResults.length > 0) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    callPhone("10086");
                } else {
                    showDialog();
                }
            }
        }
    }

    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("警告");
        builder.setMessage("\"请前往设置->应用->案例杂糅->权限中打开相关权限，否则功能无法正常运行！\"");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.create().show();
    }

    /* 拨打电话的函数*/
    private void callPhone(String num) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri uri = Uri.parse("tel:" + num);
        intent.setData(uri);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

        }
        startActivity(intent);
    }
}